<template>
    <!-- 
        今日进度(还差)
            1.商品列表
                添加/修改 页面完成步骤1
            2.订单列表
                修改/物流 弹框
            3.数据统计
                echarts 表
 -->
    <div class="addcommodity">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card class="box-card">
            <el-alert center style="margin-bottom: 15px;;" title="添加商品信息" :closable="false" type="info" show-icon>
            </el-alert>
            <el-steps align-center style="width: 100%;" :active="active - 0" finish-status="success">
                <el-step title="基本信息"></el-step>
                <el-step title="商品参数"></el-step>
                <el-step title="商品属性"></el-step>
                <el-step title="商品图片"></el-step>
                <el-step title="商品内容"></el-step>
                <el-step title="完成"></el-step>
            </el-steps>
            <el-form ref="basicinfo_ref" label-width="90px" :model="basicinfo_data" :rules="basicinfo_rules">
                <el-tabs :before-leave="beforeLeave" @tab-click="tabClick" v-model="active" style="margin-top: 25px;"
                    tab-position="left">
                    <el-tab-pane name="0" label="基本信息">
                        <!-- label 表单左侧的文字信息   prop是绑定了表单校验对象的哪一个属性-->
                        <el-form-item label="商品名称" prop="goods_name">
                            <el-input v-model="basicinfo_data.goods_name"></el-input>
                        </el-form-item>
                        <el-form-item label="商品价格" prop="goods_price">
                            <el-input type="number" v-model="basicinfo_data.goods_price"></el-input>
                        </el-form-item>
                        <el-form-item type="number" label="商品重量" prop="goods_weight">
                            <el-input v-model="basicinfo_data.goods_weight"></el-input>
                        </el-form-item>
                        <el-form-item type="number" label="商品数量" prop="hot_mumber">
                            <el-input v-model="basicinfo_data.hot_mumber"></el-input>
                        </el-form-item>
                        <el-form-item label="商品分类" prop="classification">
                            <el-cascader style="width: 100%;" clearable v-model="basicinfo_data.classification"
                                :options="selectoptions" :props="selectprop" @change="handleChange">
                            </el-cascader>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane name="1" label="商品参数">
                        <el-form-item v-for="(item, index) in checkbox" :key="'checkbox' + index"
                            :label="item.attr_name" prop="hot_mumber">
                            <el-checkbox-group v-model="item.attr_vals">
                                <el-checkbox style="margin: 0 5px 0 0 ;" v-for="(v, i) in item.attr_vals"
                                    :key="'checkbox_in' + i" :border="true" :label="v">
                                </el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane name="2" label="商品属性">
                        <el-form-item v-for="(item) in shop_info" :key="item.attr_id" label-width="140px"
                            :label="item.attr_name">
                            <el-input v-model="item.attr_vals"></el-input>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane name="3" label="商品图片">
                        <el-upload :on-remove="onRemove" :on-success="onSuccess" :headers="headers"
                            action="http://127.0.0.1:8888/api/private/v1/upload" :on-preview="handlePreview"
                            list-type="picture">
                            <el-button size="small" type="primary">点击上传</el-button>
                        </el-upload>
                    </el-tab-pane>
                    <el-tab-pane name="4" label="商品内容">
                        <!-- 富文本 -->
                        <quill-editor ref="myQuillEditor" v-model="supertxt" />
                        <el-button style="margin: 15px 0 0 0 ;" @click="addshop" type="primary">添加商品</el-button>
                    </el-tab-pane>
                </el-tabs>
            </el-form>
        </el-card>
        <el-dialog title="预览图片" :visible.sync="lookimg_dialog">
            <img style="width: 100%;" :src="lookimg" alt="">
        </el-dialog>
    </div>
</template>

<script>
//深拷贝用
import _ from 'lodash';
export default {
    data() {
        return {
            //基础信息
            basicinfo_data: {
                goods_name: '',
                goods_price: 0,
                goods_weight: 0,
                hot_mumber: 0,
                classification: [],
            },
            basicinfo_rules: {
                goods_name: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                ],
                goods_weight: [
                    { required: true, message: '请输入', trigger: 'blur' },
                ],
                hot_mumber: [
                    { required: true, message: '请输入', trigger: 'blur' },
                ],
                goods_price: [
                    { required: true, message: '请输入', trigger: 'blur' },
                ],
                classification: [
                    { required: true, message: '请输入', trigger: 'blur' },
                ],
            },
            selectoptions: [],
            selectprop: {
                label: 'cat_name',
                value: 'cat_id',
                children: 'children',
                expandTrigger: 'hover',
            },
            active: '0',
            //CheckBox 商品参数
            checkbox: [],
            //商品属性
            shop_info: [],
            //请求头
            headers: {
                Authorization: window.sessionStorage.getItem('token')
            },
            //图片
            pics: [],
            //预览
            lookimg_dialog: false,
            //
            lookimg: '',
            //富文本
            supertxt: ''
        }
    },
    created() {
        this.getselectinfo()
    },
    methods: {
        async getselectinfo() {
            let { data: res } = await this.$http.get(`categories/`);
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
            this.selectoptions = res.data
        },
        handleChange() {
            console.log(11);
        },
        async tabClick() {
            if (this.active == '1') {
                let id = this.basicinfo_data.classification[this.basicinfo_data.classification.length - 1]
                let { data: res } = await this.$http.get(`categories/${id}/attributes`, { params: { sel: 'many' } });
                if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
                res.data.forEach((item) => {
                    item.attr_vals = item.attr_vals ? item.attr_vals.split(' ') : []
                    item.checkbox = true;
                })
                this.checkbox = res.data;
            } else if (this.active == '2') {
                let id = this.basicinfo_data.classification[this.basicinfo_data.classification.length - 1]
                let { data: res } = await this.$http.get(`categories/${id}/attributes`, { params: { sel: 'only' } });
                if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
                this.shop_info = res.data;
            }

        },
        beforeLeave() {
            if (this.basicinfo_data.classification.length < 1) {
                this.$message.error('请先选择分类');
                return false
            } else {
                return true
            }
        },
        //预览事件
        handlePreview(file) {
            this.lookimg = file.response.data.url;
            this.lookimg_dialog = true;
        },
        //上传成功
        onSuccess(res) {
            let pic = { pic: res.data.tmp_path };
            this.pics.push(pic)
            console.log(this.pics);
        },
        //删除
        onRemove(file) {
            //找到
            let path = file.response.data.tmp_path;
            let weizhi = this.pics.findIndex((item) => {
                return item.pic === path
            });
            //删除
            this.pics.splice(weizhi, 1)

        },
        addshop() {
            //校验
            this.$refs.basicinfo_ref.validate(async v => {
                if (!v) return this.$message.error('表单校验错误');
                let arr = [];
                this.checkbox.forEach((item) => {
                    let obj = {
                        attr_id: item.attr_id,
                        attr_value: item.attr_vals.join(' ')
                    }
                    arr.push(obj);
                })
                this.shop_info.forEach((item) => {
                    let obj = {
                        attr_id: item.attr_id,
                        attr_value: item.attr_vals
                    }
                    arr.push(obj);
                })
                //深拷贝
                let cat = _.cloneDeep(this.basicinfo_data.classification)
                let info = {
                    goods_name: this.basicinfo_data.goods_name,
                    goods_cat: cat.join(','),
                    goods_price: this.basicinfo_data.goods_price,
                    goods_number: this.basicinfo_data.hot_mumber,
                    goods_weight: this.basicinfo_data.goods_weight,
                    goods_introduce: this.supertxt,
                    pics: this.pics,
                    attrs: arr,
                };
                let { data: res } = await this.$http.post(`goods`, info);
                if (res.meta.status !== 201) return this.$message.error(res.meta.msg);
                this.$message.success('成功');
                this.$router.push('/goods');
            });
        },
    },
}
</script>

<style>
</style>